<template>
  <div id="stackBar">
    <div ref="chart01" class="chart01"></div>
  </div>
</template>

<script>
export default {
  name: "stackBar",
  components: {},
  props: {
    pieData: Array | Object,
    pieName: String
  },
  data() {
    return {};
  },
  computed: {
    pieDataValue() {
      let arr = this.pieData.map(function (item) {
        return item.value;
      });
      return arr.reverse();
    },
    pieDataName() {
      let arr = this.pieData.map(function (item) {
        return item.name;
      });
      return arr.reverse();
    },
  },
  watch: {},
  created() {},
  mounted() {
    this.drawStackBar();
  },

  methods: {
    // js 计算百分比的方法
    // scaleValue(countArray) {
    //   var j = eval(countArray.join("+"));
    //   var resultArray = [];
    //   for (var i = 0; i < countArray.length; i++) {
    //     var k = ((countArray[i] / j) * 100).toFixed(2);
    //     resultArray.push(k);
    //   }
    //   return resultArray;
    // },
    drawStackBar() {
      let that = this;
      let mychart = that.$echarts.init(that.$refs.chart01);
      let option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "line", // 默认为直线，可选为：'line' | 'shadow'
            lineStyle: {
              color: "#e5e5e5"
            }
          },
          extraCssText: 'z-index: 9', //设置消息提示框的层级
          formatter: "{a} <br/>{b}: ({c}%)",
        },
        grid: {
          top: '0',
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "value",
            // min: 0,
            // max: 100,
            //设置网格线颜色
            splitLine: {
              show: false,
              lineStyle: {
                color: ["#e5e5e5"],
                width: 1,
                type: "dashed",
              },
            },
            axisTick: {
                show: false
            },
            // x轴样式
            axisLine: {
              show: false,
              lineStyle: {
                type: "solid",
                color: "#e5e5e5", //左边线的颜色
                width: "1", //坐标线的宽度
              },
            },
            // x轴字体样式
            axisLabel: {
              formatter: "{value} %",
              interval: 0,
              // rotate: 40,
              margin: 14,
              show: false,
              textStyle: {
                color: "#888",
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12,
              },
            },
          },
        ],
        yAxis: [
          {
            data: this.pieDataName,
            type: "category",
            //设置网格线颜色
            splitLine: {
              show: true,
              lineStyle: {
                color: ["#e5e5e5"],
                width: 1,
                type: "dashed",
              },
            },
            axisTick: {
                show: false
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: "#888",
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12,
              },
            },
            axisLine: {
              lineStyle: {
                type: "solid",
                color: "#e5e5e5", //左边线的颜色
                width: "1", //坐标线的宽度
              },
            },
          },
        ],
        series: [
          {
            name: this.pieName,
            type: "bar",
            // stack: "数量",
            data: this.pieDataValue,
            itemStyle: {
              normal: {
                // color: "#1779ff",
                color: "#f4b477",
                //柱子颜色
              },
            },
            barWidth: 14, // 控制柱子的宽度
            barGap: "-100%", //柱子重叠
          },
        ],
      };
      mychart.setOption(option);
      //   窗口自适应
      setTimeout(() => {
        window.addEventListener("resize", function () {
          mychart.resize();
        });
      }, 200);
    },
  },
};
</script>
<style lang='less' scoped>
#stackBar {
  width: 100%;
  height: 100%;
  // margin-top: 36px;
  .chart01 {
    width: 100%;
    height: 100%;
    overflow: hidden;
    clear: both;
  }
}
</style>